package com.lankystar.sos.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.lankystar.sos.model.User;
import com.lankystar.sos.service.IUserInterface;
import com.lankystar.util.ResUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@ParentPackage("sos")
@Namespace("/")
@Component
@Scope("prototype")
@Results({
	@Result(name="success",type="json")
})
public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	User user = new User();
	ActionContext ac = ActionContext.getContext();
	
	@Autowired
	private IUserInterface userInterface;
	private String userNickName;
	private String userPassword;
	private String userEmail;
	private String userPhone;
	private String userBirthday;
	private String userGender;
	private Integer userId;
	private Double  longitude;
	private Double latitude;
	private Integer type;
	public String getUserNickName() {
		return userNickName;
	}
	public void setUserNickName(String userNickName) {
		this.userNickName = userNickName;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	public String getUserEmail() {
		return userEmail;
	}
	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}
	public String getUserPhone() {
		return userPhone;
	}
	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public Double getLongitude() {
		return longitude;
	}
	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}
	public Double getLatitude() {
		return latitude;
	}
	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public String getUserBirthday() {
		return userBirthday;
	}
	public void setUserBirthday(String userBirthday) {
		this.userBirthday = userBirthday;
	}
	public String getUserGender() {
		return userGender;
	}
	public void setUserGender(String userGender) {
		this.userGender = userGender;
	}
	/**
	 * 登录 返回用户信息
	 * @return
	 */
	@Action(value="userLogin")
	public String userLoginMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			Map<String, Object> maps = userInterface.userLoginMethod(userPhone, userPassword);
			map.put("data", maps);
			map.put("code", 1);
			map.put("msg", "用户登录成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "用户登录失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	/**
	 * 注册   返回 成功信息
	 * @return
	 */
	@Action(value="userRegister")
	public String userRegisterMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			User record = userInterface.queryUserByuserPhoneMethod(userPhone);
			if(record==null){
				User user = new User();
				user.setUserNickName(userNickName);
				user.setUserPhone(userPhone);
				user.setUserPassword(userPassword);
				user.setUserCreateTime(new Date());
				user.setUserEmail(userEmail);
				userInterface.userRegisterMethod(user);
				map.put("code", 1);
				map.put("msg", "用户注册成功！！");
			}else{
				map.put("code", 0);
				map.put("msg", "用户注册失败,用户已存在！！！");
			}
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "用户注册失败！！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	/**
	 * 查找附近   求救者  、施救者信息
	 * @return
	 */
	@Action(value="queryUserlist")
	public String queryUserlistMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", userInterface.queryUserlistMethod(type,longitude,latitude));
			map.put("code", 1);
			map.put("msg", "附近用户查询成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "附近用户查询失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	/**
	 * 获取施救者位置
	 */
	@Action(value="queryUserLocation")
	public String queryUserLocationMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", userInterface.queryUserLocationMethod(userId));
			map.put("code", 1);
			map.put("msg", "用户位置查询成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "用户位置查询失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	/**
	 * 上传用户位置  userPhone、longitude、latitude;
	 * @return
	 */
	@Action(value="uploadUserLocation")
	public String uploadUserLocationMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			userInterface.userLocationUploadMethod(userPhone, longitude, latitude);
			map.put("code", 1);
			map.put("msg", "用户位置上传成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "用户位置上传失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	
	@Action(value="modifyUser")
	public String modifyUser(){
		Map<String, Object> map = new HashMap<>();
		try {
			User record = new User();
			SimpleDateFormat sdf =  new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
			record.setUserBirthday(sdf.parse(userBirthday));
			record.setUserEmail(userEmail);
			record.setUserGender(userGender);
			record.setUserNickName(userNickName);
			map.put("code", 1);
			map.put("msg", "用户信息更新成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "用户信息更新失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	
	/**
	 * 根据手机号查找求救单记录
	 * @return
	 */
	@Action(value="querySosRecord")
	public String querySosRecordMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", userInterface.querySosOrderByuserPhone(userPhone));
			map.put("code", 1);
			map.put("msg", "查询求救记录成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "查询求救记录失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
}
